package p.ithorns.support.arthas;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.LocalDateTime;
import java.util.concurrent.CountDownLatch;

/**
 * @author ithorns
 */
@EnableAsync
@EnableScheduling
@SpringBootApplication //(scanBasePackages = {"p.ithorns.framework.logger"})
public class AsApp {

    private final Logger log = LoggerFactory.getLogger(AsApp.class);

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("*")
                        .allowedMethods("GET", "POST", "PUT", "DELETE")
                        .allowedHeaders("*");
            }
        };
    }

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(AsApp.class, args);
        // new CountDownLatch(1).await();
    }

   // @Scheduled(fixedDelay = 2000L)
    public void schedule() throws InterruptedException {
        //  System.out.println("-==========> 定时打印内容: " + LocalDateTime.now());
       CountDownLatch latch = new CountDownLatch(1);
       new Thread(() -> {
           while (true) {
               log.info("Hello, {}!", LocalDateTime.now());
               try {
                   Thread.sleep(5000L);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
       }, "log-producer").start();
       latch.await();
    }

}